package com.gjy.amqp.simple.delayed;

import com.gjy.amqp.util.RabbitmqUtil;
import com.rabbitmq.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-05-17 11:32:44
 */
public class DelayedConsumer {

    private static final Logger log = LoggerFactory.getLogger(DelayedConsumer.class);
    private static final String DELAYED_DLX_QUEUE = "dlx_queue";

    public static void main(String[] args) throws Exception {
        Connection connection = RabbitmqUtil.connection();
        Channel channel = connection.createChannel();

        channel.basicConsume(DELAYED_DLX_QUEUE, false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                log.info("延时队列接收消息: {}, {}", new String(body, StandardCharsets.UTF_8), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss")));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });

        log.info("延时队列消费成功");
    }

}
